﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Matrix.Configuration.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ConfigurationCacheItems",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Value = table.Column<byte[]>(type: "bytea", nullable: false),
                    ExpiryTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CacheKey = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
                    ValueType = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    ExtraProperties = table.Column<string>(type: "text", nullable: false),
                    ConcurrencyStamp = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    CreationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
                    LastModificationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    LastModifierId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ConfigurationCacheItems", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "ConfigurationItems",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    Namespace = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
                    Key = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
                    Value = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: false),
                    DataType = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    Category = table.Column<string>(type: "text", nullable: false),
                    Description = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
                    Version = table.Column<long>(type: "bigint", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    ExtraProperties = table.Column<string>(type: "text", nullable: false),
                    ConcurrencyStamp = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    CreationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
                    LastModificationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    LastModifierId = table.Column<Guid>(type: "uuid", nullable: true),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    DeleterId = table.Column<Guid>(type: "uuid", nullable: true),
                    DeletionTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ConfigurationItems", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationCacheItems_CacheKey",
                table: "ConfigurationCacheItems",
                column: "CacheKey",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationCacheItems_ExpiryTime",
                table: "ConfigurationCacheItems",
                column: "ExpiryTime");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_BatchLookup_Tenant_Namespace_Key_ModifiedTime",
                table: "ConfigurationItems",
                columns: new[] { "TenantId", "Namespace", "Key", "LastModificationTime" });

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_Composite_Tenant_Namespace_Key_Deleted",
                table: "ConfigurationItems",
                columns: new[] { "TenantId", "Namespace", "Key", "IsDeleted" });

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_CreationTime_TenantId",
                table: "ConfigurationItems",
                columns: new[] { "CreationTime", "TenantId" });

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_Key",
                table: "ConfigurationItems",
                column: "Key");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_Namespace",
                table: "ConfigurationItems",
                column: "Namespace");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_Namespace_Key_NotDeleted",
                table: "ConfigurationItems",
                columns: new[] { "Namespace", "Key" },
                filter: "\"IsDeleted\" = false");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_TenantId_Namespace_Key",
                table: "ConfigurationItems",
                columns: new[] { "TenantId", "Namespace", "Key" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_TenantId_NotDeleted",
                table: "ConfigurationItems",
                column: "TenantId",
                filter: "\"IsDeleted\" = false");

            migrationBuilder.CreateIndex(
                name: "IX_ConfigurationItems_Version",
                table: "ConfigurationItems",
                column: "Version");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "ConfigurationCacheItems");

            migrationBuilder.DropTable(
                name: "ConfigurationItems");
        }
    }
}
